package com.rsy.crm.workbench.web.controller;

import com.rsy.crm.settings.domain.User;
import com.rsy.crm.settings.service.Impl.UserServiceImpl;
import com.rsy.crm.settings.service.UserService;
import com.rsy.crm.util.DateTimeUtil;
import com.rsy.crm.util.PrintJson;
import com.rsy.crm.util.ServiceFactory;
import com.rsy.crm.util.UUIDUtil;
import com.rsy.crm.vo.PageVo;
import com.rsy.crm.workbench.domain.Activity;
import com.rsy.crm.workbench.domain.Clue;
import com.rsy.crm.workbench.domain.Tran;
import com.rsy.crm.workbench.service.ActivityService;
import com.rsy.crm.workbench.service.ClueService;
import com.rsy.crm.workbench.service.Impl.ActivityServiceImpl;
import com.rsy.crm.workbench.service.Impl.ClueServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 线索控制器
 */
public class ClueController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("线索控制器");
        String path = request.getServletPath();


        if("/workbench/clue/pageList.do".equals(path)){
            pageList(request,response);
        }else if("/workbench/clue/getUserList.do".equals(path)){
            getUserList(request,response);
        }else if("/workbench/clue/save.do".equals(path)){
            saveClue(request,response);
        }else if("/workbench/clue/detail.do".equals(path)){
            detailClue(request,response);
        }else if ("/workbench/clue/getActivityListByClueId.do".equals(path)){
            getActivityListByClueId(request,response);
        }else if("/workbench/clue/unbund.do".equals(path)){
            unbund(request,response);
        }else if("/workbench/clue/getActivityListByNameAndNotByClueId.do".equals(path)){
            getActivityListByNameAndNotByClueId(request,response);
        }else if("/workbench/clue/bund.do".equals(path)) {
            bund(request, response);
        }else if("/workbench/clue/getActivityListByName.do".equals(path)){
            getActivityListByName(request,response);
        }else if("/workbench/clue/convert.do".equals(path)){
            convert(request,response);
        }
    }

    /**
     * 转换
     * @param request
     * @param response
     */
    private void convert(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("线索转换");
        String clueId =request.getParameter("clueId");
        //接收是否需要创建交易的标记
        String flag = request.getParameter("flag");
        //获取联系人
        String createBy = ((User)request.getSession().getAttribute("user")).getCreateBy();
        //交易表
        Tran tran = null;

        if ("true".equals(flag)){
            tran = new Tran();

            //接受交易表单中的信息
            //接收交易表单中的参数
            String money = request.getParameter("money");
            String name = request.getParameter("name");
            String expectedDate = request.getParameter("expectedDate");
            String stage = request.getParameter("stage");
            String activityId = request.getParameter("activityId");

            String id = UUIDUtil.getUUID();
            String createTime = DateTimeUtil.getSysTime();



            tran.setId(id);
            tran.setMoney(money);
            tran.setName(name);
            tran.setExpectedDate(expectedDate);
            tran.setStage(stage);
            tran.setActivityId(activityId);
            tran.setCreateBy(createBy);
            tran.setCreateTime(createTime);

        }
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        boolean success = clueService.convert(clueId,tran,createBy);
        if (success){
            response.sendRedirect(request.getContextPath()+"/workbench/clue/index.jsp");
        }
    }



    /**
     * 查询市场活动列表（根据名称模糊查）
     * @param request
     * @param response
     */
    private void getActivityListByName(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("查询市场活动列表（根据名称模糊查）");

        String aname = request.getParameter("aname");


        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        List<Activity> aList = as.getActivityListByName(aname);

        PrintJson.printJsonObj(response, aList);
    }

    /**
     * 执行关联市场活动的操作
     * @param request
     * @param response
     */
    private void bund(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("执行关联市场活动的操作");

        String cid = request.getParameter("cid");
        String aids[] = request.getParameterValues("aid");

        ClueService cs = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        boolean flag = cs.bund(cid,aids);

        PrintJson.printJsonFlag(response, flag);
    }


    /**
     * 模糊查询活动
     * @param request
     * @param response
     */
    private void getActivityListByNameAndNotByClueId(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("查询市场活动列表（根据名称模糊查+排除掉已经关联指定线索的列表）");

        String aname = request.getParameter("aname");
        String clueId = request.getParameter("clueId");

        Map<String,String> map = new HashMap<String,String>();
        map.put("aname", aname);
        map.put("clueId", clueId);

        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        List<Activity> aList = as.getActivityListByNameAndNotByClueId(map);

        PrintJson.printJsonObj(response, aList);

    }

    /**
     * 删除关联
     * @param request
     * @param response
     */
    private void unbund(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("执行解除关联操作");

        String id = request.getParameter("id");

        ClueService cs = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        boolean flag = cs.unbund(id);

        PrintJson.printJsonFlag(response, flag);
    }

    /**
     * 线索 与 活动关联
     * @param request
     * @param response
     */
    private void getActivityListByClueId(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("根据线索id查询关联的市场活动列表");

        String clueId = request.getParameter("clueId");

        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        List<Activity> aList = as.getActivityListByClueId(clueId);

        PrintJson.printJsonObj(response, aList);
    }

    /**
     * 跳转到线索详细信息页
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void detailClue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("跳转到线索详细信息页");

        String id = request.getParameter("id");

        ClueService cs = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        Clue c = cs.detail(id);

        request.setAttribute("c", c);
        request.getRequestDispatcher("/workbench/clue/detail.jsp").forward(request, response);

    }

    /**
     * 添加线索
     * @param request
     * @param response
     */
    private void saveClue(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("执行线索添加操作");

        String id = UUIDUtil.getUUID();

        String fullname = request.getParameter("fullname");
        String appellation = request.getParameter("appellation");
        String owner = request.getParameter("owner");
        String company = request.getParameter("company");
        String job = request.getParameter("job");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String website = request.getParameter("website");
        String mphone = request.getParameter("mphone");
        String state = request.getParameter("state");
        String source = request.getParameter("source");

        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User)request.getSession().getAttribute("user")).getName();

        String description = request.getParameter("description");
        String contactSummary = request.getParameter("contactSummary");
        String nextContactTime = request.getParameter("nextContactTime");
        String address = request.getParameter("address");

        Clue c = new Clue();
        c.setId(id);
        c.setAddress(address);
        c.setWebsite(website);
        c.setState(state);
        c.setSource(source);
        c.setPhone(phone);
        c.setOwner(owner);
        c.setNextContactTime(nextContactTime);
        c.setMphone(mphone);
        c.setJob(job);
        c.setFullname(fullname);
        c.setEmail(email);
        c.setDescription(description);
        c.setCreateTime(createTime);
        c.setCreateBy(createBy);
        c.setContactSummary(contactSummary);
        c.setCompany(company);
        c.setAppellation(appellation);

        ClueService cs = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        boolean flag = cs.save(c);

        PrintJson.printJsonFlag(response, flag);



    }

    /**
     * 添加操作获取用户信息列表
     * @param request
     * @param response
     */
    private void getUserList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("取得用户信息列表");

        UserService us = (UserService) ServiceFactory.getService(new UserServiceImpl());

        List<User> uList = us.getUserList();

        PrintJson.printJsonObj(response, uList);


    }

    /**
     * 线索分页
     * @param request
     * @param response
     */
    private void pageList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入条件 + 分页查询");
        //第几页
        Integer pageNo    = Integer.valueOf(request.getParameter("pageNo"));
        //每页的记录个数
        Integer pageSize  = Integer.valueOf(request.getParameter("pageSize"));
        String  fullname      = request.getParameter("fullname");
        String  company     = request.getParameter("company");
        String  phone = request.getParameter("phone");
        String  source   = request.getParameter("source");
        String  owner   = request.getParameter("owner");
        String  mphone   = request.getParameter("mphone");
        String  state   = request.getParameter("state");

        //每次查询需要过滤的个数
        int skipCount = (pageNo-1)*pageSize;

        Map<String,Object> map = new HashMap<>();
        map.put("skipCount",skipCount);
        map.put("pageSize",pageSize);
        map.put("fullname",fullname);
        map.put("company",company);
        map.put("phone",phone);
        map.put("source",source);
        map.put("owner",owner);
        map.put("mphone",mphone);
        map.put("state",state);

        ClueService cs = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        PageVo<Activity> pageVo = cs.pageList(map);

        PrintJson.printJsonObj(response,pageVo);
    }
}
